package com.gec.dao.impl;

import com.gec.dao.UserDao;
import com.gec.domain.User;
import com.gec.utils.JDBCUtils;
import com.gec.utils.PageModel;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //添加用户
    @Override
    public void addUser(User user) {
        String sql="INSERT INTO user_inf(loginname,PASSWORD,STATUS,createdate,username)VALUES(?,?,?,?,?)";
        Object[] params={user.getLoginname(), user.getPassword(), user.getStatus(), user.getCreatedate(), user.getUsername()};
        try {
            JDBCUtils.executeUpdate(sql,params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
    }

    //修改用户
    @Override
    public void updateUser(User user) {
        String sql="UPDATE user_inf SET loginname=?,PASSWORD=?,username=? WHERE id=?";
        Object[]params={user.getLoginname(),user.getPassword(),user.getUsername(),user.getId()};
        try{
            JDBCUtils.executeUpdate(sql,params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
    }

    //删除用户
    @Override
    public void delete(int id) {
        String sql="DELETE FROM user_inf WHERE id=?";
        Object[] params={id};
        try {
            JDBCUtils.executeUpdate(sql,params);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
    }

    //查看所有用户
    @Override
    public List<User> findAllUsers(User users,PageModel model) {
        List<User>user1=new ArrayList<>();

        //动态查询
        StringBuffer sql=new StringBuffer("select * from user_inf where 1=1 ");
        List<Object>objects=new ArrayList<>();

        ResultSet rs=null;
        try {
            //防止空指针异常
            if(users !=null && !"".equals(users.getLoginname()) && users.getLoginname() !=null){
                sql.append(" and loginname like ?");
                objects.add("%"+users.getLoginname()+"%");
            }
            //分页查询
            sql.append(" limit ").append(model.getStartRow()).append(",").append(PageModel.pageSize);

            //形成数组
            Object[] params = objects.toArray(new Object[objects.size()]);

            rs=JDBCUtils.executeQuery(sql.toString(),params);

            while(rs.next()){
                User user=new User();
                copy(rs,user);
                user1.add(user);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
        return user1;
    }

    //总的信息数
    @Override
    public int getTotalUserCount(User user) {
        StringBuffer sql=new StringBuffer("SELECT COUNT(0) FROM user_inf WHERE 1=1  ");

        List<Object>objects=new ArrayList<>();
        ResultSet rs=null;
        try{
            if(user != null && !"".equals(user.getLoginname()) && user.getLoginname() !=null){
                sql.append("  and loginname like ?");
                objects.add("%" +user.getLoginname() +"%");
            }
            //分页查询
            //动态参数数组 转换成数组
            Object[]params=objects.toArray(new Object[objects.size()]);
            rs=JDBCUtils.executeQuery(sql.toString(),params);

            while (rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),rs);
        }
        return 0;
    }

    @Override
    public User findUserInfo(String loginname) {
        String sql="SELECT * FROM user_inf WHERE loginname=?";
        Object[]params={loginname};
        ResultSet rs=null;
        try{
            rs=JDBCUtils.executeQuery(sql,params);
            while(rs.next()){
                User user=new User();
                copy(rs,user);
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
        return null;
    }

    //登录时查看账户密码是否正确
    @Override
    public User login(String loginname, String password) {
        String sql="SELECT * FROM user_inf WHERE loginname=? AND PASSWORD=?";
        Object[]params={loginname,password};
        ResultSet rs=null;
        try{
            rs=JDBCUtils.executeQuery(sql,params);
            while(rs.next()){
                User user=new User();
                copy(rs,user);
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
        return null;
    }

    //查询个人信息
    @Override
    public User findById(int id) {
        String sql="SELECT * FROM user_inf WHERE id=?";
        Object[]params={id};
        ResultSet rs=null;
        try {
            rs=JDBCUtils.executeQuery(sql,params);
            while(rs.next()){
                User user=new User();
                copy(rs,user);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(JDBCUtils.getConn(),JDBCUtils.getPs(),null);
        }
        return null;
    }

    private void copy(ResultSet rs, User user) {
        try {
            user.setId(rs.getInt("id"));
            user.setLoginname(rs.getString("loginname"));
            user.setPassword(rs.getString("password"));
            user.setStatus(rs.getInt("status"));
            user.setCreatedate(rs.getDate("createdate"));
            user.setUsername(rs.getString("username"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
